package com.qu.Media.media.adpater;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qu.Media.R;
import com.qu.Media.media.bean.Image;
import com.qu.Media.media.bean.ImageFolder;
import com.qu.Media.media.config.ImageLoaderListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 显示图片菜单的Adapter
 * Created by quguangle on 2017/4/18.
 */

public class ImageFolderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private final LayoutInflater inflater;
    private List<ImageFolder> datas;
    private ImageLoaderListener loader;
    public ImageFolderAdapter(Context mContext){
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
        if(datas == null){
            datas = new ArrayList<>();
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ImageFolderVewHolder(inflater.inflate(R.layout.item_list_folder, parent, false),
                mContext);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ImageFolderVewHolder imageFolderVewHolder = (ImageFolderVewHolder) holder;
        imageFolderVewHolder.setData(position,datas.get(position));
    }
    public void setLoader(ImageLoaderListener loader) {
        this.loader = loader;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ImageFolderVewHolder extends RecyclerView.ViewHolder{
        private Context mContext;
        public ImageView iv_folder;
        public TextView tv_folder_name,tv_size;
        public ImageFolderVewHolder(View itemView,Context mContext) {
            super(itemView);
            this.mContext = mContext;
            iv_folder = (ImageView) itemView.findViewById(R.id.iv_folder);
            tv_folder_name = (TextView) itemView.findViewById(R.id.tv_folder_name);
            tv_size = (TextView) itemView.findViewById(R.id.tv_size);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onImageFolderItemClick != null){
                        onImageFolderItemClick.onItemClick(getLayoutPosition());
                    }
                }
            });
        }
        public void setData(int position,ImageFolder item){
            tv_folder_name.setText(item.getName());
            tv_size.setText(String.format("(%s)",item.getImages().size()));
            if(loader != null){
                loader.displayImage(iv_folder,item.getAlbumPath());
            }
        }
    }
    private OnImageFolderItemClick onImageFolderItemClick;
    public interface OnImageFolderItemClick{
        void onItemClick(int position);
    }
    public void setOnImageFolderItemClick(OnImageFolderItemClick onImageFolderItemClick){
        this.onImageFolderItemClick = onImageFolderItemClick;
    }

    public ImageFolder getItem(int position){
        if(position >=0 && position< datas.size()){
            return datas.get(position);
        }
        return null;
    }
    public final void resetItem(List<ImageFolder> items) {
        if (items != null) {
            clear();
            addAll(items);
        }
    }
    public final void clear() {
        this.datas.clear();
        notifyDataSetChanged();
    }
    public void addAll(List<ImageFolder> items) {
        if (items != null) {
            this.datas.addAll(items);
            notifyItemRangeInserted(this.datas.size(), items.size());
        }
    }
}
